package function;

import View.AddFriendWindow;
import View.AddGroupWindow;
import View.MainWindow;
import View.window;
import client.ClientManager;
import instruction.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;

public class AddGroupFunction {
    public static Object $(window window, String id) {
        return (Object) window.getRoot().lookup("#" + id);
    }
    public static void searchGroupExec(AddGroupWindow addGroupWindow, MainWindow mainWindow, ClientManager clientManager){
        ((Label)$(addGroupWindow,"errorTip")).setText("");
        ((Button)$(addGroupWindow,"addGroup")).setOnAction(event->{
            boolean flag=true;
            User user=mainWindow.getUserData().getUser();
            Group group=addGroupWindow.getGroup();
            String userName=user.getName();
            String userAccount=user.getAccount();
            String userImage=user.getHead();
            String userNickname=user.getNickname();
            String userAge=user.getAge();
            String userSex=user.getSex();
            String userBirthday=user.getBirthday();
            String userSign=user.getSignature();
            String userPhone=user.getPhone();
            String userEmail=user.getEmail();
            String userAddress=user.getAddress();
            Member userMember=new Member(userName,userAccount,userSex,userBirthday,userSign,userEmail,userAge,userAddress,userNickname,userPhone,userImage);
            GroupMember groupMember=new GroupMember(group,userMember);
            Message message=new Message("addGroup");
            for(Group group1:mainWindow.getUserData().getGroupList())
            {
                if(group.getAccount().equals(group1.getAccount())){
                    flag=false;
                    break;
                }

            }
            message.setGroupMember(groupMember);
            if(flag) {
                clientManager.getSend().send(message);
                addGroupWindow.hide();
            }else{
                ((Label)$(addGroupWindow,"errorTip")).setText("您不能重复加入该群");
            }
        });
    }
}
